<template>
	<view>
		<view class="content">
			<view class="company" v-for="(item,index) in factory" :key="index">
				<view class="company__box" hover-class="clickable" @click="selectFactory(item,index)">
					<view class="radio-box">
						<custom-radio :checked="item.checked"></custom-radio>
					</view>
					<view class="">
						{{item.aname}}
					</view>
				</view>
			</view>
			<view class="confirm" v-show="type == 'checkbox'">
				<u-button type="primary" text="确定" color="#2979ff" @click="confirmSelect"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import _custom from '@/common/_custom'
	import CustomRadio from '@/components/custom/radio/radio.vue'
	
	export default {
		components: {
			CustomRadio
		},
		data() {
			return {
				factory: [],
				selectedFactory: [],
				type: 'checkbox',//单选：radio，多选：checkbox
				name: 'select-factory',//uni.$on()接收数据时的名称
			}
		},
		onLoad(options) {
			if(options.name) this.name = options.name;
			if(options.type) this.type = options.type;
			if(options.factorys) this.selectedFactory = JSON.parse(options.factorys);
			this.getTeamWorkOBS()
		},
		methods: {
			getTeamWorkOBS() {
				let that = this;
				_custom.getDataJava({
					apiname: 'getTeamWorkOBS',
					success(res) {
						res.forEach(item => {
							item.locked = false;
							item.checked = false;
							let flag = false;
							that.selectedFactory.forEach(v => {
								if(item.OBSCODE == v.OBSCODE) flag = true;
							})
							if(flag) {
								item.checked = true;
								item.locked = true;
							}
						})
						that.factory = res;
					}
				})
			},
			/**
			 * 选择水厂
			 * @param {Object} v
			 */
			selectFactory(v) {
				if(v.locked) return
				if(this.type == 'radio') {
					let checked = !v.checked;
					this.$set(v,'checked',checked);
					this.confirmSelect()
					return
				}
				let checked = !v.checked;
				this.$set(v,'checked',checked)
			},
			/**
			 * 确认选择结果
			 */
			confirmSelect() {
				let select = [];
				this.factory.forEach(item => {
					if(item.checked) select.push(item)
				})
				this.selectedFactory = select;
				uni.$emit(this.name,this.selectedFactory);
				setTimeout(() =>{
					uni.navigateBack({
						delta: 1
					})
				},300)
			},
		}
	}
</script>

<style>
	
	.content {
		margin-bottom: 100rpx;
	}
	
	.company {
		background-color: #fff;
		font-size: 30rpx;
		border-bottom: 1rpx solid #eee;
	}
	
	.company__box {
		display: flex;
		height: 100rpx;
		align-items: center;
	}
	
	.open {
		overflow: hidden;
		transition: all 1s cubic-bezier(0.25, 1.0, 0.25, 1.0);
	}
	
	.close {
		height: 0;
		transition: all 0.4s cubic-bezier(0.25, 1.0, 0.25, 1.0);
	}
	
	.radio-box {
		width: 80rpx;
	}
	
	.confirm {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>
